package com.xindaoapp.happypet.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.FansActivity;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.activity.MenuAttention;
import com.xindaoapp.happypet.activity.MyLoveActivity;
import com.xindaoapp.happypet.activity.MyMessageActivity;
import com.xindaoapp.happypet.activity.MyOrderActivity;
import com.xindaoapp.happypet.activity.MyPostActivity;
import com.xindaoapp.happypet.activity.MyShopCollectActivity;
import com.xindaoapp.happypet.activity.MyShouCangActivity;
import com.xindaoapp.happypet.activity.SettingActivity;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;

/**
 * 个人中心
 * 
 * @author chenghao
 * 
 */
public class PersonalCenterFragment extends BaseFragment implements OnClickListener {
    /** 登录按钮 */
    private View vLogin;
    /** 用户名 */
    private TextView vUserName;
    /** 用户头像 */
    private ImageView vUserIcon;

    private LoginSuccessReceiver mLoginSuccessReceiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_personal_center, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLoginSuccessReceiver = new LoginSuccessReceiver();
        getActivity().registerReceiver(mLoginSuccessReceiver,
                new IntentFilter(Constants.ACTION_LOGIN_STATE_CHANGED));
        setUserInfos();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mLoginSuccessReceiver);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        // initTopBg(view);
        vLogin = view.findViewById(R.id.login);
        vUserName = (TextView) view.findViewById(R.id.user_name);
        vUserIcon = (ImageView) view.findViewById(R.id.user_icon);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        vLogin.setOnClickListener(this);
        view.findViewById(R.id.r_top).setOnClickListener(this);
        view.findViewById(R.id.setting).setOnClickListener(this);
        view.findViewById(R.id.tab_invitation).setOnClickListener(this);
        view.findViewById(R.id.tab_attention).setOnClickListener(this);
        view.findViewById(R.id.tab_fans).setOnClickListener(this);
        view.findViewById(R.id.tab_mylove).setOnClickListener(this);
        view.findViewById(R.id.tab_mycollect).setOnClickListener(this);
        view.findViewById(R.id.tab_mymessage).setOnClickListener(this);
        view.findViewById(R.id.tab_myorder).setOnClickListener(this);
        view.findViewById(R.id.tab_mygoods).setOnClickListener(this);
        view.findViewById(R.id.tab_myshipping_address).setOnClickListener(this);
    }

    /**
     * 
     */
    private void initTopBg(View view) {
        LayoutParams params =
                new LayoutParams(CommonParameter.sScreenWidth,
                        CommonParameter.sScreenWidth * 327 / 640);
        view.findViewById(R.id.r_top).setLayoutParams(params);
    }

    private void setUserInfos() {
        if (UserState.isLogged()) {
            vLogin.setVisibility(View.GONE);
            vUserName.setVisibility(View.VISIBLE);
            vUserIcon.setVisibility(View.VISIBLE);
            vUserName.setText(UserState.getUser().username);
            ImageLoader.getInstance().displayImage(UserState.getUser().userface, vUserIcon);
        } else {
            vLogin.setVisibility(View.VISIBLE);
            vUserName.setVisibility(View.GONE);
            vUserIcon.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean showBottomTabBar() {
        return false;
    }

    @Override
    protected boolean showTopTabBar() {
        return false;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.r_top:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            MyInfoFragment.class.getCanonicalName());
                    startActivity(intent);
                }
                break;
            case R.id.login:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            LoginFragment.class.getCanonicalName());
                    startActivity(intent);
                }
                break;
            case R.id.setting:
                intent.setClass(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tab_invitation:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyPostActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_attention:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MenuAttention.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_fans:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), FansActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_mylove:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyLoveActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_mycollect:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyShouCangActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_mymessage:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyMessageActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_myorder:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyOrderActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_mygoods:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MyShopCollectActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tab_myshipping_address:
                if (UserState.isLogged(getActivity())) {
                    intent.setClass(getActivity(), MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            ConsigneeFragment.class.getCanonicalName());
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    class LoginSuccessReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            setUserInfos();
        }

    }

}
